/*
* LibraryServiceImpl.java
* Created on  2014-7-30 下午2:35
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-7-30       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.task.dao.LibraryDao;
import com.genghis.ptas.task.entity.LibraryOption;
import com.genghis.ptas.task.entity.LibraryTask;
import com.genghis.ptas.task.service.LibraryService;
import com.genghis.ptas.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Transactional
@Service("libraryService")
public class LibraryServiceImpl implements LibraryService {
    @Autowired
    private LibraryDao libraryDao;

    @Autowired
    private DictService dictService;

    @Override
    public Page<LibraryTask> findLibraryTaskPageList(PageParameter pageParameter, LibraryTask libraryTask) {
        Page<LibraryTask> libraryTaskPage = libraryDao.findLibraryTaskPageList(pageParameter, libraryTask);
        for (LibraryTask libraryTask1 : libraryTaskPage.getResultList()) {
            libraryTask1.setShowTaskType(dictService.changeDictCodeToValue(libraryTask1.getTaskType(), DictConstant.TASKTYPE));
        }
        return libraryTaskPage;
    }

    @Override
    public void deleteLibraryTaskById(Integer id) {
        libraryDao.deleteLibraryTaskById(id);
    }

    @Override
    public int addLibraryTask(LibraryTask libraryTask) {
        String[] content = libraryTask.getCollectOptions().split("@#");
        initLibraryTask(content.length, libraryTask);
        int taskId = libraryDao.addLibraryTask(libraryTask);
        LibraryOption libraryOption = new LibraryOption(taskId);
        for (int i = 0; i < content.length; i++) {
            libraryOption.setOptionContent(content[i]);
            libraryDao.addLibraryOption(libraryOption);
        }
        return taskId;
    }

    @Override
    public LibraryTask findLibraryTaskById(int taskId) {
        LibraryTask libraryTask = libraryDao.findLibraryTaskById(taskId);
        libraryTask.setOptions(libraryDao.findLibraryOptionByTaskId(taskId));
        return libraryTask;
    }

    @Override
    public LibraryTask analyzeLibraryTask(LibraryTask libraryTask) {
        String[] option = libraryTask.getCollectOptions().split("\n");
        String optionContent = "";
        for (int i = 0, first = 0; i < option.length; i++) {
            if (!option[i].equals("") && first == 1) {
                optionContent += option[i] + "@#";
            } else if (!option[i].equals("")) {
                libraryTask.setTitle(option[0]);
                first = 1;
            }
        }
        if (optionContent.length() > 0) {
            libraryTask.setCollectOptions(optionContent.substring(0, optionContent.length() - 2));
        } else {
            libraryTask.setCollectOptions(optionContent);
        }
        return libraryTask;
    }

    @Override
    public void editLibraryTask(LibraryTask libraryTask) {
        libraryDao.deleteLibraryOptionByTaskId(libraryTask.getId());
        String[] content = libraryTask.getCollectOptions().split("@#");
        LibraryOption libraryOption = new LibraryOption(libraryTask.getId());
        for (int i = 0; i < content.length; i++) {
            libraryOption.setOptionContent(content[i]);
            libraryDao.addLibraryOption(libraryOption);
        }
        libraryDao.editLibraryTask(libraryTask);
    }

    private void initLibraryTask(Integer number, LibraryTask libraryTask) {
        libraryTask.setOptionNumber(number);
        libraryTask.setUpDate(DateUtil.getSystemDate("yyyy-MM-dd HH:mm:ss"));
    }
}
